(*
	setSPortBufferSize sizeOfBuffer -- Free the old buffer (if any) and allocate a new on of the size given.
		The buffer is permanently freed if the size is zero, or by calling closeSPort.

	Note: If this routine is called with a non-zero buffer size, it is imperative that closeSPort is
	called or bufferSPort is called again with a zero buffer size, before exiting HyperCard. If this
	is not done, the serial port driver will continue to dump characters into the buffer despite the
	fact that the buffer and been deallocated as a part of the application heap deallocation. This
	means the data will get written to "random" locations used by the Finder or the next application.
	This, in turn, will cause strange and mysterious crashes.

	To compile and link this file using Macintosh Programmer's Workshop,

		pascal -w setSPortBufferSize.p
		link -m ENTRYPOINT -o HyperCommands -rt XCMD=7034 -sn Main=setSPortBufferSize ¶
			setSPortBufferSize.p.o "{MPW}"Libraries:interface.o

	© Copyright 1987,88 by Apple Computer, Inc.

	Initial coding 9/87 by Harry R. Chesley.
*)

{$R-}

{$S setSPortBufferSize } 	{ Segment name must be the same as the command name. }

unit DummyUnit;

interface

uses MemTypes, QuickDraw, OSIntf, ToolIntf, HyperXCmd;

procedure EntryPoint(paramPtr: XCmdPtr);
	
implementation

type

Str31 = String[31];

procedure setSPortBufferSize(paramPtr: XCmdPtr); forward;

procedure EntryPoint(paramPtr: XCmdPtr);

	begin
		setSPortBufferSize(paramPtr);
	end;

procedure setSPortBufferSize(paramPtr: XCmdPtr);

	var newBufferSize: longInt;
		newBufferPtr: Ptr;

	{$I XCmdGlue.inc}

	procedure Fail(errMsg: Str255); { set theResult and quit }
		begin
			paramPtr^.returnValue := PasToZero(errMsg);
			exit(setSPortBufferSize);
		end;

	{$I SPortUtil.inc}

	begin
		if paramPtr^.paramCount <> 1 then Fail('parameter count is not 1');

		newBufferSize := GetLongParm(1);

		SetUpSPortGlobals;
		EnsureOpenPort;

		{ Check if we're to use the default buffer. }
		if newBufferSize = 0 then newBufferPtr := nil
		{ If not, create the new buffer. }
		else
			begin
				newBufferPtr := NewPtr(newBufferSize);
				if newBufferPtr = nil then newBufferSize := 0;
			end;
		{ Let the port know about the buffer. }
		if SerSetBuf(ThisSPort.portInDev,newBufferPtr,newBufferSize) <> noErr then
			begin
				DisposPtr(newBufferPtr);
				Fail('SetSerBuf failed');
			end;
		{ If there was an old buffer, dispose of it. }
		if ThisSPort.inputBuffer <> NIL then DisposPtr(ThisSPort.inputBuffer);
		{ Remember the new buffer's location. }
		Globals^^.ports[Globals^^.selectedPort].inputBuffer := newBufferPtr;
	end;
end.
